package com.bluetooth.xunxun.util;


import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;


public class UI_Util {
	public static boolean checkWifi(Context context) {
		boolean isWifiConnect = true;
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
		for (int i = 0; i < networkInfos.length; i++) {
			if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
				if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
					isWifiConnect = false;
				}
				if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
					isWifiConnect = true;
				}
			}
		}
		return isWifiConnect;
	}

	public static void setBtnTouchState(View btn, int resNor, int resDown) {
		final int rNor = resNor;
		final int rDown = resDown;
		btn.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent arg1) {
				int i = arg1.getAction();
				if (i == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(rDown);
				} else if (i == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(rNor);
				} else if (i == MotionEvent.ACTION_CANCEL) {
					arg0.setBackgroundResource(rNor);
				}
				return false;
			}
		});
	}
	
	
	
	
	public static void setBtnTouchState(final ImageView btn, int resNor, int resDown) {
		final int rNor = resNor;
		final int rDown = resDown;
		btn.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent arg1) {
				int i = arg1.getAction();
				if (i == MotionEvent.ACTION_DOWN) {
					btn.setImageResource(rDown);
				} else if (i == MotionEvent.ACTION_UP) {
					btn.setImageResource(rNor);
				} else if (i == MotionEvent.ACTION_CANCEL) {
					btn.setImageResource(rNor);
				}
				return false;
			}
		});
	}

	public static void setBtnTouchState(View btn, int resNor, int resDown, final String textColorUp, final String textColorDown) {
		final int rNor = resNor;
		final int rDown = resDown;

		btn.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg, MotionEvent arg1) {
				final Button arg0 = (Button) arg;
				int i = arg1.getAction();
				if (i == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(rDown);
					arg0.setTextColor(Color.parseColor(textColorDown));

				} else if (i == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(rNor);
					arg0.setTextColor(Color.parseColor(textColorUp));
				} else if (i == MotionEvent.ACTION_CANCEL) {
					arg0.setBackgroundResource(rNor);
					arg0.setTextColor(Color.parseColor(textColorUp));
				}
				return false;
			}
		});
	}

	public static void setBtnClickable(View btn, boolean clickable, String ableTextColor, int ableBgColor, String unableTextColor, int unableBgColor) {
		Button arg0 = (Button) btn;
		if (clickable) {
			arg0.setBackgroundResource(ableBgColor);
			arg0.setTextColor(Color.parseColor(ableTextColor));
		} else {
			arg0.setBackgroundResource(unableBgColor);
			arg0.setTextColor(Color.parseColor(unableTextColor));
		}
		arg0.setClickable(clickable);
	}


}
